# 游戏2048
import random
list_merge = None
"""
[2,0,2,2]  [4,2,0,0]
[0,0,4,4]  [8,0,0,0]
[2,4,2,0]  [2,4,2,0]
[2,2,0,2]  [4,2,0,0]
"""
map = [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]
]
"""
[0][1] <==> [1][0]  [0][2] <==> [2][0]  [0][3] <==> [3][0]
[1][2] <==> [2][1]  [1][3] <==> [3][1]
[2][3] <==> [3][2]
"""
# 矩阵翻转
def square_matrix_transpose():
    for i in range(0, len(map) - 1):
        for j in range(i+1, len(map)):
            map[i][j],map[j][i] = map[j][i],map[i][j]
# 删除所有值为n的列表
def zero_to_end():
    for i in range(-1, -len(list_merge) - 1, -1):
        if list_merge[i] == 0:
            del list_merge[i]
            list_merge.append(0)
def merge():
    zero_to_end()
    for i in range(len(list_merge) - 1):
        if list_merge[i] == 0:
            continue
        if list_merge[i] == list_merge[i+1]:
            list_merge[i] *= 2
            del list_merge[i+1]
            list_merge.append(0)
# 左移
def move_left(key):
    if key != 1:
        return
    for line in map:
        global list_merge
        list_merge = line
        merge()
# 右移
def move_right(key):
    if key != 3:
        return
    for line in map:
        global list_merge
        list_merge = line[::-1]
        merge()
        line[:] = list_merge[::-1]
# 上移
def move_up(key):
    if key != 5:
        return
    square_matrix_transpose()
    move_left(1)
    square_matrix_transpose()
# 下移
def move_down(key):
    if key != 2:
        return
    square_matrix_transpose()
    move_right(3)
    square_matrix_transpose()
# 生成一个2
def get_num():
    lst = []
    for i, value1 in enumerate(map):
        for j, value2 in enumerate(map):
            if map[i][j] == 0:
                lst.append([i, j])
    index = random.randint(0, len(lst) - 1)
    map[lst[index][0]][lst[index][1]] = 2
    return [lst[index][0], lst[index][1]]
# 打印地图
def print_map():
    lst = get_num()
    # 打印不同颜色的字
    # print(f"\033[1;31;10m2\033[0m")
    for x, item1 in enumerate(map):
        print("[", end="")
        for y, item2 in enumerate(item1):
            if lst and lst[0] == x and lst[1] == y:
                # print(f"\033[1;31;10m{这里填要打印的值，下面的是2}\033[0m", end="\t")
                # 格式 \033[显示方式;前景色;背景色m
                """
                显示方式：
                0 || "0" || mormal
                1 || "1" || bold
                4 || "4" || underline
                5 || "5" || blink
                7 || "7" || invert
                8 || "8" || hide
                --------------------------------
                前景色
                30 || "30" || black
                31 || "31" || red
                32 || "32" || geen
                33 || "33" || yellow
                34 || "34" || blue
                35 || "35" || purple
                36 || "36" || cyan
                37 || "37" || white
                示例：
                \033[1;31;40m2\033[0m  高亮显示，前景色红色，背景色黑色
                """
                print(f"\033[1;31;10m2\033[0m", end="\t")
            else:
                if item1[y] != 0:
                    print(f"\033[1;33;10m{item1[y]}\033[0m", end="\t")
                else:
                    print(item1[y], end="\t")
        print("]")
while True:
    print_map()
    key = int(input("输入：5、上移 2、下移 1、左移 3、右移 0、退出："))
    if key == 0:
        break
    move_up(key)
    move_down(key)
    move_left(key)
    move_right(key)


# def zero_to_end(lst):
#     new_list = list(filter(lambda x: x != 0, lst))
#     for i in range(lst.count(0)):
#         new_list.append(0)
#     return new_list

# def merge(lst):
#     new_list = zero_to_end(lst)
#     for index in range(len(new_list) - 1):
#         if new_list[index] == 0:
#             break
#         if new_list[index] == new_list[index+1]:
#             new_list[index] *= 2
#             del new_list[index+1]
#             new_list.append(0)
#     return new_list


# while True:
#     for item in my_lists:
#         print(item)
#     n = input()
#     if int(n) == 0:
#         break
#     for index in range(len(map)):
#         new_list = merge(my_lists[index])
#         my_lists[index] = new_list
